//
//  WYPublicWeb.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/23.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "WYPublicWeb.h"
//获取充值页面的配置信息
#define PEIZHI_URL @"/api/ApiPay/getRechargeConfig"
#import "PayForDbbVC.h"
#import "UMengRgt.h"
@interface WYPublicWeb ()<UIWebViewDelegate,UMSocialUIDelegate>
{
    //网页
    UIWebView *_webView;
    //进度提示
    UIActivityIndicatorView *flower;
    NSInteger count;
}
@end

@implementation WYPublicWeb

- (void)viewDidLoad {
    [super viewDidLoad];
    count = 0;
    [self setHeader];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 44, WYMWITH, WYMHEIGHT-44)];
    [self.view addSubview:_webView];
    _webView.delegate = self;
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
    [self loadCustomNavigationBar];
}
//设置请求头
- (void)setHeader
{
    //设置请求头
    NSString *deviceModel = [UIDevice currentDevice].model;/***设备型号******/
    float iosBanben = [[[UIDevice currentDevice] systemVersion] floatValue];/*****系统版本********/
    /**********软件版本**********/
    //版本
    NSString *app_v = @"V1.0.0";
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if (app_Version != nil) {
        app_v = [NSString stringWithFormat:@"V%@",app_Version];
    }
    //唯一标示devicetoken
    NSString *uuid = @"";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user objectForKey:@"IDFA"]) {
        uuid = [user objectForKey:@"IDFA"];
    }
    NSString *agent = [NSString stringWithFormat:@"db8/%@/iOS/%.2f/%@/%@",app_v,iosBanben,deviceModel,uuid];
    
    NSDictionary *dictionary = @{@"UserAgent": agent};
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
/*************
 *加载navigationbar
 ***************/
- (void)loadCustomNavigationBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 64)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    [self.view addSubview:naviBar];
    //左按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(5, 25, 30, 30);
    [leftBtn setImage:[UIImage imageNamed:@"icon_back_white_press"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [naviBar addSubview: leftBtn];
    //中间标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 30)];
    titleLab.center = CGPointMake(WYMWITH/2, 20+44/2);
    titleLab.text = self.mtitle;
    titleLab.textColor = [UIColor whiteColor];
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [naviBar addSubview: titleLab];
    
}
//返回按钮监听事件
- (void)backBtnOnClick:(UIButton *)btn
{
    if([_webView canGoBack]){
        [_webView goBack];
        
    }else{
        if (_isDIsmissFLag) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    flower = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0,0, 100, 100)];
    flower.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
    flower.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.view addSubview:flower];
    [flower startAnimating];
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    /*absoluteString;
     relativeString;
     parameterString*/
//    NSLog(@"url=%@",[NSString stringWithFormat:@"%@",request.URL]);
    NSString *urlstr = [NSString stringWithFormat:@"%@",request.URL];
    //://1.dbba.cn/?/dbba_register_award
    /***********跳往去充值***********/
    if ([urlstr rangeOfString:@"dbba_register_award"].location != NSNotFound ){
        if (count >= 1) {
            [self goToPay];
        }
        count++;
    }
    /******************跳往分享***************/
    if ([urlstr rangeOfString:@"dbba_share_award"].location != NSNotFound ){
        if (count >= 1 && [WYPublic ifUserHaveLogin]) {
            //注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
            [UMengRgt shareWithVC:self delegate:self];
        }
        count++;
    }
    return YES;
}
//跳往充值页面
- (void)goToPay
{
    if ([WYPublic ifUserHaveLogin]) {
        [WYPublic addZheZhaoToWindowWithTitle:nil];
        NSDictionary *params = @{@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,PEIZHI_URL] Parameters:params successBlock:^(NSDictionary *dic) {
            NSNumber *ret = dic[@"ret"];
            if (ret.intValue == 0) {
                PayForDbbVC *payDbbVC = [[PayForDbbVC alloc]init];
                payDbbVC.peizhiDic = dic[@"data"];
                [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:payDbbVC animated:YES];
            }else{
                if(ret.intValue>2000 && ret.intValue < 3000){
                    //用户过期，强制重新登录 先删除用户信息
                    [UserInfo deleteUserInfo];
                    //登录
                    [WYPublic ifUserHaveLogin];
                    [WYPublic showMessageWithTitle:@"提示：" Msg:@"登录信息已过期"];
                }
                
            }
            [WYPublic removeZheZhaoToWindow];
        } failedBlock:^{
            [WYPublic removeZheZhaoToWindow];
        } needLog:NO];

    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [flower stopAnimating];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    
    NSLog(@"WYPublicWeb dealloc");
}


@end
